package com.evil.common.application.enums.robot;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;
import java.util.function.Consumer;

/**
 * 机器人规则源类型
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum RobotRuleSourceTypeEnum {

    /**
     * 机器人规则源类型（1-流程节点机器人规则）
     */
    PROCESS_NODE_ROBOT(1, "流程节点机器人规则", RobotSourceTypeEnum.PROCESS_NODE_ROBOT),
    CUSTOM_BUTTON_RULE(2, "自定义按钮赋值规则", RobotSourceTypeEnum.BUTTON),
    APPLICATION_ROBOT(3, "应用机器人规则", RobotSourceTypeEnum.APPLICATION_ROBOT),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;
    @Getter
    private final RobotSourceTypeEnum robotSourceTypeEnum;

    public static Optional<RobotRuleSourceTypeEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static RobotRuleSourceTypeEnum isExistById(Integer id) {
        return RobotRuleSourceTypeEnum.findById(id).orElseThrow(RobotRuleSourceTypeEnum::getBusinessException);
    }

    public static void forEach(Consumer<RobotRuleSourceTypeEnum> consumer) {
        Arrays.asList(values()).forEach(consumer);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "规则源类型参数错误");
    }
}
